'''last update @2025-05-07
  暂时只做简单运行,运行md游戏,megadriv为日版和欧版,genesis为美版和世界版
'''

import os
import sys
import subprocess

from MdRoms import *

### 常量定义
MAME = r'mame.exe'
#PARAMS = '-plugin autofire,inputmacro,cheat,cheatfind -window -keyboardprovider win32'
#PARAMS = '-window -console -keyboardprovider win32 -plugin inputmacro'
#PARAMS += r' -console'
#PARAMS += r' -language Chinese_Simplified -console'
PARAMS = [
  '-plugin', 'autofire,inputmacro,cheat,cheatfind',
  '-window', '-console',
  '-keyboardprovider', 'win32'
]

### 变量定义
inpText = '' # 接受输入的字符层
index = 0 # 输入编号
romName = '' # 选择的rom文件
command = '' # 命令行指令

### 脚本执行
print('执行MAME0.273版本的md游戏:')
while((index > -1) and (index < len(ROMS))):
  ## 初始化
  ## 提示信息显示
  i = 0 # 临时变量
  while (i < len(ROMS)):
    if (i == len(ROMS)-1):
      print(f'{i+1:3} : {ROMS[i][1]:20}')
    else:
      print(f'{i+1:3} : {ROMS[i][1]:20}   {i+2:3} : {ROMS[i+1][1]:20}')
    i += 2
  ## 输入ROM编号,回车退出
  try:
    print('')
    inpText = input("输入rom编号并按回车执行,直接回车退出:")
    if (inpText in ROMS_SHOTCUT):
      romName = ROMS_SHOTCUT[inpText]
    else:
      index = int(inpText)-1
      romName = ROMS[index][0]
  except:
    exit(1)
  ## 游戏类型判断,美版和世界版用genesis,日版和欧版用megadriv
  gameType = "megadriv"
  lowerRomName = romName.lower()
  if ('(usa)' in lowerRomName) or ('(world)' in lowerRomName):
    gameType = 'genesis'
  ## rom的完全路径
  romFullPath = f'{sys.path[0]}\\roms\\{gameType}\\{romName}.zip\\{romName}.md'
  ## 执行mame命令行
  command = [MAME, gameType, "-cart", romFullPath] + PARAMS
  subprocess.run(command, shell=True)
  print('\n')
  
## 结束
exit(0)
